package org.atguigu.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
public class CaptchaController {
    @RequestMapping("/common/captcha")
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/png");//响应类型的格式时图片
        //生成一个图片验证码
        //1.生成图片
        //圆圈干扰线
//        ICaptcha captcha = CaptchaUtil.createCircleCaptcha(100, 30, 4, 4, 1);
//        ICaptcha captcha = CaptchaUtil.createCircleCaptcha(100, 30, new MathGenerator(4), 4);
        //直线干扰线
//        ICaptcha captcha = CaptchaUtil.createLineCaptcha(100, 30, 4, 4);
        //动态图
        ICaptcha captcha = CaptchaUtil.createGifCaptcha(100, 30, 4);
        //2.数字保存起来，用于验证
        request.getSession().setAttribute("captcha", captcha.getCode());
        //3.生成的图片以IO流写到浏览器
        captcha.write(response.getOutputStream());
    }
}
